package com.ia.aistream.manager.operating.util;

import com.ia.aistream.common.utils.StringUtils;
import com.ia.aistream.manager.operating.util.style.DocStyleUtil;
import com.spire.doc.*;
import com.spire.doc.collections.DocumentObjectCollection;
import com.spire.doc.collections.SectionCollection;
import com.spire.doc.documents.DocumentObjectType;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.DocPicture;
import com.spire.doc.fields.ShapeGroup;
import com.spire.doc.fields.ShapeObject;

import java.util.*;

/***
 * 实现文件内容检查
 * @author Ymj
 */
public class FileCheck {

    public static void main(String[] args) throws Exception {
        String fileName = "D:/a/check.doc";
        List<String> list = checkFile(fileName);
        System.out.println(list);
    }

    /****
     * 对File文件进行检查
     * @param fileName
     * @return
     */
    public static List<String> checkFile(String fileName) throws Exception{

        Document document = new Document();
        document.loadFromFile(fileName);
        // 记录异常Size
        List<String> result = new ArrayList<>();
        String titleName = "";
        boolean flag = true;

        SectionCollection sections = document.getSections();
        for (int i=0; i<sections.getCount(); i++){
            Section section = sections.get(i);
            DocumentObjectCollection childObjects = section.getBody().getChildObjects();
            for (int j=0; j<childObjects.getCount(); j++){
                // 判断数据是否为表格
                if(childObjects.get(j).getDocumentObjectType().equals(DocumentObjectType.Table)){
                    Table table = (Table)childObjects.get(j);
                    // 处理表格数据
                    Map<String, Object> map = checkTableException(table, titleName);
                    String tName = (String) map.get("titleName");
                    if(!tName.equals(titleName)){
                        flag = true;
                    }
                    titleName = tName;
                    result.addAll((List<String>)map.get("tableList"));
                }else if (childObjects.get(j).getDocumentObjectType().equals(DocumentObjectType.Paragraph)){
                    Paragraph paragraph = (Paragraph) childObjects.get(j);
                    int title = DocStyleUtil.isTitle(paragraph);
                    if(title!=0){
                        flag = true;
                    }
                    if(flag){
                        // 处理段落数据
                        Map<String, Object> map = checkParagraphException(paragraph, titleName);
                        List<String> list = (List<String>) map.get("parList");
                        result.addAll(list);
                        String tName = (String) map.get("titleName");
                        if(list.size()>0){
                            flag = false;
                        }
                        titleName = tName;
                    }
                }
            }
        }
        return result;
    }

    /***
     * 检查表格异常
     * @param table
     * @return
     */
    public static Map<String,Object> checkTableException(Table table, String titleName){
        List<String> tableList = new ArrayList<>();
        Map<String,Object> map1 = new HashMap<>();
//        String titleName = "";
        // 遍历行数据
        for (int r=0; r<table.getRows().getCount(); r++){
            TableRow tableRow = table.getRows().get(r);
            // 遍历列数据
           for (int c=0; c<tableRow.getCells().getCount(); c++){
               TableCell tableCell = tableRow.getCells().get(c);
               // 遍历段落数据
               for (int p=0; p<tableCell.getParagraphs().getCount(); p++){
                   Paragraph paragraph = tableCell.getParagraphs().get(p);
                   Map<String, Object> map = checkTabParException(paragraph, titleName, tableRow.getCells().getCount());
                   List<String> parList = (List<String>) map.get("tabList");
                   titleName = (String) map.get("titleName");
                   if(parList!=null){
                       tableList.addAll(parList);
                   }
               }
           }
        }
        map1.put("tableList", tableList);
        map1.put("titleName", titleName);
        return map1;
    }

    /***
     * 实现表格段落的异常识别
     * @param paragraph
     * @return
     */
    public static Map<String, Object> checkTabParException(Paragraph paragraph, String titleName, int colMax){
        List<String> parList = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        String content = paragraph.getListText().trim() + " " + paragraph.getText().trim();
        int title = DocStyleUtil.isTitle(paragraph);
        if(!StringUtils.isEmpty(content) && title!=0){
            titleName = content;
        }
        
        for (int o=0; o<paragraph.getChildObjects().getCount(); o++){
            DocumentObject object = paragraph.getChildObjects().get(o);
            if(object instanceof ShapeGroup){
                String msg = titleName + ": 下发生画图异常\r\n";
                parList.add(msg.trim());
            } else if(object instanceof DocPicture && colMax==1){
                String msg = titleName + ": 表格中嵌套图片";
                parList.add(msg.trim());
            }
        }
        map.put("tabList", parList);
        map.put("titleName", titleName);
        return map;
    }
    /***
     * 实现段落数据异常处理
     * @param paragraph
     * @return
     */
    public static Map<String, Object> checkParagraphException(Paragraph paragraph, String titleName) throws Exception {
        List<String> parList = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        String content = paragraph.getListText().trim() + " " + paragraph.getText().trim();
        int title = DocStyleUtil.isTitle(paragraph);
        if(!StringUtils.isEmpty(content) && title!=0){
            titleName = content;
        }
        for (int o=0; o<paragraph.getChildObjects().getCount(); o++){
            DocumentObject object = paragraph.getChildObjects().get(o);
            if(object instanceof ShapeGroup){
                String msg = titleName + ": 下存在ShapeGroup异常\r\n";
                if (!parList.contains(msg.trim())){
                    parList.add(msg.trim());
                }
                break;
            }else if(object instanceof ShapeObject){
                ShapeObject shapeObject = (ShapeObject)object;
                String s = shapeObject.getShapeType().toString();
                if (s.equals("Line")){
                    String msg = titleName + ": 下可能存在流程图，请转为Visio进行处理\r\n";
                    if (!parList.contains(msg.trim())){
                        parList.add(msg.trim());
                    }
                    break;
                }
            }
        }
        map.put("parList", parList);
        map.put("titleName", titleName);
        return map;
    }
}
